package
{
	import com.yahoo.webapis.weather.*;
	import com.yahoo.webapis.weather.events.WeatherErrorEvent;
	import com.yahoo.webapis.weather.events.WeatherResultEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import org.aswing.*;
	
	/**
	 * ...
	 * @author yangfan1122@gmail.com
	 * 
	 * 从yahoo取得默认的数据，构建界面
	 * 从界面下拉框选择新地点，请求数据，重新构建JFrame里内容
	 * ps.选择一个地点后，发送事件，文档类记录该地点，重新构建界面时，将该地点设为默认选中
	 * 
	 */
	public class YahooWeatherForecast extends Sprite
	{
		private var myIconLoader:Loader;
		private var myEvent:Event;
		private var weather_service:WeatherService=new WeatherService();
		private var weatherInfo:Weather;
		
		private var pane:CreateTaskPane;
		private var frame:JFrame;
		private var weatherContent:Object = {};
		private var comboObj:Object = {};
		private var citySelectNo:int;
		
		public function YahooWeatherForecast()
		{
			init();
		}
		
		private function init():void
		{
			AsWingManager.initAsStandard(this);
			getCities();
			frame = new JFrame(this, "天气预报");
			
			weather_service.getWeather("CHXX0046", Units.METRIC_UNITS);//默认哈尔滨
			weather_service.addEventListener(WeatherResultEvent.WEATHER_LOADED, onWeatherLoaded);
		}
		
		
		//城市代码
		private function getCities():void
		{
			var _namesArr:Array = [];
			var _objArr:Array = [];
			
			for(var i:Object in Cities.cities.city)
			{
				_namesArr.push(Cities.cities.city[i].@name);
				_objArr.push({name:Cities.cities.city[i].@name, id:Cities.cities.city[i].@id});
			}
			_namesArr.unshift("选择");
			_objArr.unshift({name:"选择", id:""});
			comboObj = {names:_namesArr, obj:_objArr};
		}
		
		
		// //城市代码
		
		//天气
		private function onWeatherLoaded(e:WeatherResultEvent):void
		{
			weatherInfo=weather_service.weather;
			
			weatherContent = {
				"city" : weatherInfo.location.city,
					"description" : weatherInfo.current.description,
					"temperature" : weatherInfo.current.temperature + "°C",
					"pic" : weatherInfo.current.imageURL,
					"tomorrow_description" : weatherInfo.forecast[1].description,
					"tomorrow_high" : weatherInfo.forecast[1].high + "°C",
					"tomorrow_low" : weatherInfo.forecast[1].low + "°C"
			};
			
			appInterface();
		}
		
		// //天气
		
		
		//interface
		private function appInterface():void
		{
			pane = new CreateTaskPane();
			pane.addEventListener("CITY_SELECTED", setCity);
			pane.createTaskPaneHandler(weatherContent, comboObj);
			
			frame.setTitle("天气预报 - "+pane.city);
			frame.setContentPane(pane);
			frame.pack();
			frame.width = 200;
			frame.height = 350;
			frame.show();
			//trace("frame.numChildren="+frame.numChildren);//6
			
			if(citySelectNo == 0)citySelectNo = 69;//默认哈尔滨
			pane.setCombIndex(citySelectNo);//combobox当前显示项
		}
		private function setCity(event:Event):void
		{
			//trace(event.target.selectedCityId+"~~~~~~~~~~~~~");//重新向yahoo请求数据
			
			pane.removeEventListener("CITY_SELECTED", setCity);
			//frame.removeAll();//??!!
			citySelectNo = event.target.cityNo;	
			weather_service.getWeather(event.target.selectedCityId, Units.METRIC_UNITS);
			
		}
		
		
		// //interface
		
		
		
		
		
	}
}


















